/*
 * @Author: mikey.zhaopeng
 * @Date:   2016-01-14 16:11:23
 * @Last Modified by:   mikey.zhaopeng
 * @Last Modified time: 2016-07-13 16:53:39
 * @打开大图编辑
 */

'use strict';
define([
    '../../videoManage/base/baseView',
    '../../videoManage/model/video',
    'jquery-html5Validate'
], function(
    BaseView,
    videoModel
) {
    function me() {
        BaseView.apply(this, arguments);
        var self = this;
        this.init = function(id) {
            self.id = id;
            self.$el = $('#video-container');
            self.render();
            self.event();
        }
        this.event = function() {
            $('#btn-save').off('click').on('click', $.proxy(self.updateAdvideos, self));
        }

        this.render = function() {
            videoModel.getAdvideoByIds(self.id, function(result) {
                self.$editEl = self.$el.find('.video-edit-container');
                self._.each(result, function(vid) {
                    var html = self.t('#t-videoes-edit', vid);
                    self.$editEl.append(html);
                });
                self.result = result;
                self.renderApp();
                self.renderLanguage();
                self.renderSex();
            });
        }



        this.updateAdvideos = function(e) {
            var params = [];
            var  isPass=false;
            self.$el.find('.video-row').each(function(i, v) {
                var appNames = $(this).find('.app-select2').val();
                if(!appNames || appNames.length==0){
                    $(this).find('.app-select2').next().testRemind('请选择应用!');
                    if(!isPass){
                        isPass=true;
                    }
                }
                var vid = {
                    mongodbId: $(this).data('mid'),
                    name: $(this).find('.file-name-title>span').text(),
                    appNames:appNames,
                    languages: $(this).find('.language-select2').val() || [],
                    gender: $(this).find('.sex-select2').val() || 0,
                }

                if (vid.gender.length == 0) {
                    vid.gender = '0';
                }

                params.push(vid);
            });


            if(isPass){
                return;
            }

            videoModel.updateAdvideos(params, function(result) {
                setTimeout(function() {
                    window.location.hash = '!videoManage';
                }, 600);
            });


        }


        this.renderApp = function() {
            videoModel.queryApps(function(apps) {
                var optionHtml = '';
                self._.each(apps, function(app) {
                    optionHtml = optionHtml + '<option value="' + app.name + '"> ' + app.name + ' </option>';
                });
                self.$el.find('.app-select2').html(optionHtml);
                self.$el.find('.app-select2').select2({
                    placeholder: "请选择应用"
                });
                self.setApp();
            });
        }

        this.setApp = function() {
            self.$el.find('.video-row').each(function(i, v) {
                var params = {
                    mongodbId: $(this).data('mid')
                }
                var vid = self._.findWhere(self.result, params);
                if (vid && vid.tags && vid.tags.application && vid.tags.application[0]) {
                    if (_.indexOf(vid.tags.application, self.appName) == -1) {
                        vid.tags.application.push(self.appName);
                    }
                    $(this).find('.app-select2').select2('val', vid.tags.application);
                }
            })
        }


        this.renderLanguage = function() {
            videoModel.queryLanguage(function(languages) {
                var optionHtml = '';
                self._.each(languages, function(lang) {
                    optionHtml = optionHtml + '<option value="' + lang.name + '"> ' + lang.name + ' </option>';
                });
                self.$el.find('.language-select2').html(optionHtml);
                self.$el.find('.language-select2').select2({
                    placeholder: "请选择语言"
                });
                self.setLanguage();
            });
        }

        this.setLanguage = function() {
            self.$el.find('.video-row').each(function(i, v) {
                var params = {
                    mongodbId: $(this).data('mid')
                }
                var vid = self._.findWhere(self.result, params);
                if (vid && vid.tags && vid.tags.languages && vid.tags.languages[0]) {
                    $(this).find('.language-select2').select2('val', vid.tags.languages);
                }
            })
        }



        this.renderSex = function() {
            self.$el.find('.sex-select2').select2();
            self.setSex();
        }

        this.setSex = function() {
            self.$el.find('.video-row').each(function(i, v) {
                var params = {
                    mongodbId: $(this).data('mid')
                }
                var vid = self._.findWhere(self.result, params);
                if (vid && vid.tags && vid.tags.gender && vid.tags.gender[0]) {
                    $(this).find('.sex-select2').select2('val', vid.tags.gender);
                }
            })
        }


    };
    return new me();
});

